<?php

namespace app\admin\controller\separate_code;

use app\admin\model\SeparateCode;
use app\api\model\SeparateSet;
use app\common\controller\Backend;
use app\common\library\job\MessageSendJob;
use think\Queue;
use think\Request;
use think\Session;
use wechat\MiniProgramService;

class Generate extends Backend
{
    /**
     * @var mixed|string
     */
    private $service_secret;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->service_secret = Session::get('admin')['secret'];
        $this->model = new SeparateCode();
        if (empty($this->service_secret))
            $this->service_secret = "Jp)GuX=M^KthZV7^";
    }

    public function index(){
        return $this->view->fetch();
    }
    public function code(){
        $data = $this->request->param("row/a");
        $arr = [];
        for ($i = 0; $i < $data['length']; $i++) {
            $code_id = uniqid($i);
            $arr[] = [
                "service_secret"=>$this->service_secret,
                "number" => $code_id,
                "web_code" => "http://vip.juanboss.com/qrcode/build?text=$code_id",
                "mini_code" => "/path/to/qrcode/$code_id.png"
            ];
        }
        // 创建后异步创建码
        Queue::push(MessageSendJob::class, ["service_secret"=>$this->service_secret,'data'=>$arr],
            'message');
        $this->model->insertAll($arr);
        $this->success("创建成功","separate_code/generate/index");
    }
}